package c.a.a.c0;

import android.content.Context;
import c.b.a.a.a;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/* compiled from: NetworkCache */
public class b {
    public final Context a;

    public b(Context context) {
        this.a = context.getApplicationContext();
    }

    public static String a(String str, a aVar, boolean z) {
        StringBuilder g = a.g("lottie_cache_");
        g.append(str.replaceAll("\\W+", ""));
        if (z) {
            Objects.requireNonNull(aVar);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(".temp");
            stringBuilder.append(aVar.a);
            str = stringBuilder.toString();
        } else {
            str = aVar.a;
        }
        g.append(str);
        return g.toString();
    }

    public final File b() {
        File file = new File(this.a.getCacheDir(), "lottie_network_cache");
        if (file.isFile()) {
            file.delete();
        }
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    public File c(String str, InputStream inputStream, a aVar) throws IOException {
        File file = new File(b(), a(str, aVar, true));
        FileOutputStream fileOutputStream;
        try {
            fileOutputStream = new FileOutputStream(file);
            byte[] bArr = new byte[1024];
            while (true) {
                int read = inputStream.read(bArr);
                if (read != -1) {
                    fileOutputStream.write(bArr, 0, read);
                } else {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    inputStream.close();
                    return file;
                }
            }
        } catch (Throwable th) {
            inputStream.close();
        }
    }
}
